package db;

//import java.math.BigDecimal;
import java.sql.*; 
//import java.util.*;

//import org.apache.log4j.*;
/**
 * @author Mohamed Rafea
 * This class was designed to encapsulate some JDBC functions that are used for executing
 * prepared statements. The class functions include opening and closing a database connection, and execution of 
 * select queries. 
 */
public final class PreparedQuery {
	
	/** @modelguid {BECE23A7-6986-4E8C-9A67-EE7FA6C02867} */
	private Connection con;
	/** @modelguid {8376F55C-CD2E-4AE7-91FE-83AAAB4E91EA} */
	private PreparedStatement stmt;		
	
/**
 * Constructor 
 * @modelguid {6C771E60-5967-4B46-8B49-BDBA142E377A}
 */
public PreparedQuery() { }
/**
 * Close the database connectivity.
 * @modelguid {E972F4F0-0100-47B7-9DBB-8078A4CC7386}
 */
public void close() throws SQLException {
			
		
		if (con != null)
		{
			/*
			if (con.getAutoCommit()==false)
				con.commit();
			*/		
			if (stmt!=null) 
			{
				stmt.close();			
				ConnectionManager.releaseConnection(con);
				stmt = null;
			}
		}
		else if (stmt!=null) 
		{
			stmt.close();
			stmt = null;
		}
		
	
}
/**
 * This method was designed to allow the usage of one prepared statement for variable number of 
 * parameters. It takes the number of parameters to iclude in the prepared statement, an array 
 * containing the parameters' values and the dummy value to use for extra parameters' values(if any)
 */
public ResultSet executeQuery(int parametersNumber,String[] s,String dummy) throws SQLException{
	int i=1;
	for (;i<=s.length;i++)
	{
		stmt.setString(i,s[i-1]);
	}
	for (;i<=parametersNumber;i++)
	{
		stmt.setString(i,dummy);
	}
	return stmt.executeQuery();
}
/**
 * This method executes the sql previously prepared.
 */
public int executeUpdate() throws SQLException{	
	//System.out.println("**************************");
	System.out.println(stmt);
	return stmt.executeUpdate();
}
/**
 * This method executes the sql previously prepared.
 */
public ResultSet executeQuery() throws SQLException{
//	System.out.println("add user statment:");
	System.out.println(stmt);
	return stmt.executeQuery();
}
/**
 * Opens the database connectivity
 */
public void open() throws SQLException,ClassNotFoundException {
	//assert false;
	con = ConnectionManager.getConnection();
	//assert false;
}
/**
 * Opens the database connectivity and prepare the sql
 * @modelguid {9F566322-1D09-483D-BB28-28B86060DD83}
 */
public void open(String sql) throws SQLException,ClassNotFoundException {
	con = ConnectionManager.getConnection();	
	//l.debug("Preparing "+sql);
	stmt = con.prepareStatement(sql);
}
public void open(String sql,int resultSetType, int resultSetConcurrency) throws SQLException,ClassNotFoundException {
	con = ConnectionManager.getConnection();	
	//l.debug("Preparing "+sql);
	stmt = con.prepareStatement(sql,resultSetType,resultSetConcurrency);
}
/**
 * prepare the sql
 */
public void prepare(String sql) throws SQLException {
	
	//l.debug(sql);	
//	System.out.println(sql);
	stmt = con.prepareStatement(sql);
}
public void prepare(String sql,int resultSetType, int resultSetConcurrency) throws SQLException {
	
	//l.debug(sql);	
	stmt = con.prepareStatement(sql,resultSetType,resultSetConcurrency);
}
public void setParameter(int index,int value) throws SQLException
{
	//l.debug("Setting a parameter value to "+value);
	stmt.setInt(index,value);	
}
public void setParameter(int index,Time value) throws SQLException
{
	//l.debug("Setting a parameter value to "+value);
	stmt.setTime(index,value);	
}
public void setParameter(int index,float value) throws SQLException
{
	//l.debug("Setting a parameter value to "+value);
	stmt.setFloat(index,value);	
}
public void setParameter(int index,double value) throws SQLException
{
	//l.debug("Setting a parameter value to "+value);
	stmt.setDouble(index,value);	
}
public void setParameter(int index,long value) throws SQLException
{
	//l.debug("Setting a parameter value to "+value);
	stmt.setLong(index,value);	
}
public void setParameter(int index,boolean value) throws SQLException
{
	byte b = 0;
	if (value)
		b = 1;
	//stmt.setBoolean(index,value);	
	//l.debug("Setting a parameter value to "+b);
	stmt.setByte(index,b);	
}
public void setParameter(int index,byte value) throws SQLException
{
	//l.debug("Setting a parameter value to "+value);
	stmt.setByte(index,value);	
}
public void setParameter(int index,String value) throws SQLException
{
	//l.debug("Setting a parameter value to "+value);
	stmt.setString(index,value);	
}
public void setParameter(int index,char value) throws SQLException
{
	//l.debug("Setting a parameter value to "+value);
	stmt.setString(index,String.valueOf(value));
	//stmt.setString(index,String.valueOf(value));
	//System.out.println(String.valueOf(value));	
	//stmt.setString(index,"'"+String.valueOf(value)+"'");
}
public void addBatch() throws Exception
{
	stmt.addBatch();
}
public void executeBatch() throws Exception
{
	stmt.executeBatch();
}

public void beginTransaction() throws SQLException {
	con.setAutoCommit(false);
}

public void endTransaction() throws SQLException {
	con.commit();
	con.setAutoCommit(true);
}

public void cancelTransaction() throws SQLException {
	con.rollback();
	con.setAutoCommit(true);
}
public void setParameter(byte index, Timestamp created) throws SQLException {
	//l.debug("Setting a parameter value to "+created);
	stmt.setTimestamp(index,created);	

	
}
}
